package com.base.self.vos;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.comm.CommUrls;
import com.base.mapper.voipmapper.ChangeRecordMapper;
import com.base.po.voip.ChangeRecord;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class VosRequest {

    private final ChangeRecordMapper changeRecordMapper;

    public VosRequest(ChangeRecordMapper changeRecordMapper) {
        this.changeRecordMapper = changeRecordMapper;
    }

    public boolean refundVos(final Users users, final Subscribers subscribers, final String context, final BigDecimal money) {

        try {
            VosModelList vosModelList1 = null;
            try {
                vosModelList1 = GetVosAccount("{accounts:['" + users.getAgentprefix() + subscribers.getMobile() + "']}");
            } catch (Exception e) {
                e.printStackTrace();
                log.warn("查询用户话费余额失败");
            }
            final VosModelList vosModelList = sendVos(users, subscribers, context, money);
            if (vosModelList == null) {
                return false;
            }
            if (vosModelList.retCode == 0) {
                changeRecordMapper.insertSelective(new ChangeRecord(subscribers.getParentid(),
                        subscribers.getShopid(), subscribers.getId(), DateUtil.date(), vosModelList1 == null ? BigDecimal.ZERO : vosModelList1.infoCustomers[0].getMoney(),
                        money, 0, users.getEndtime(), users.getEndtime(), "退款所扣", null, null, 0, null,
                        null, "退款所扣", null));
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("用户：{}，退款扣话费失败。", subscribers.getId());
        }
        return false;
    }

    public boolean partakerVos(final Users users, final Subscribers subscribers, final String context, final BigDecimal money) {

        try {
            final VosModelList vosModelList = sendVos(users, subscribers, context, money);
            if (vosModelList == null) {
                return false;
            }
            if (vosModelList.retCode == 0) {
                VosModelList vosModelList1 = null;
                try {
                    vosModelList1 = GetVosAccount("{accounts:['" + users.getAgentprefix() + subscribers.getMobile() + "']}");
                } catch (Exception e) {
                    e.printStackTrace();
                    log.warn("查询用户话费余额失败");
                }
                changeRecordMapper.insertSelective(new ChangeRecord(subscribers.getParentid(),
                        subscribers.getShopid(), subscribers.getId(), DateUtil.date(), vosModelList1 == null ? BigDecimal.ZERO : vosModelList1.infoCustomers[0].getMoney(),
                        money, 1, users.getEndtime(), users.getEndtime(), "拼团获取", null, null, 1, null,
                        null, "拼团获取", null));
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("用户：{}，奖励话费失败。", subscribers.getId());
        }
        return false;
    }

    /*查询账户*/
    public VosModelList GetVosAccount(String data) {
        final String body = HttpRequest
                .post(CommUrls.BaseUrl + "GetCustomer")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                .body(data)
                .execute()
                .body();
        log.warn(body);
        if (StringUtils.isEmpty(body)) {
            return null;
        }
        return JSONObject.parseObject(body, VosModelList.class);
    }

    public VosModelList sendVos(final Users users,
                                final Subscribers subscribers,
                                final String context,
                                final BigDecimal money) {
        final Map<String, Object> params = new HashMap<>();
        params.put("ownerName", users.getAgentprefix() + subscribers.getMobile());
        params.put("ownerType", 2);
        params.put("money", money);
        params.put("memo", subscribers.getMobile() + context + money + "元");

        final String body = HttpRequest
                .post(CommUrls.BaseUrl + "Pay")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                .body(JSON.toJSONString(params))
                .execute()
                .body();
        log.warn(body);
        if (StringUtils.isEmpty(body)) {
            log.warn("用户：{}，奖励话费失败。", subscribers.getId());
            return null;
        }
        return JSONObject.parseObject(body, VosModelList.class);
    }
}
